//
//  TSCardBindViewController.swift
//  TSProfitProject
//
//  Created by TMS on 2019/11/5.
//  Copyright © 2019 TMS. All rights reserved.
//

import UIKit

protocol TSCardBindViewControllerDelegate : NSObjectProtocol {
    func completeBindCard()
}

class TSCardBindViewController: TSBaseViewController {

    weak var delegate : TSCardBindViewControllerDelegate? = nil
    
    var tableView : UITableView!
    var bankField : UITextField!
    var nameField : UITextField!
    var bankLocField : UITextField!
    var bankAddressField : UITextField!
    var pwdField : UITextField!
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        configViews()
        setupNavigationTitle(title: "银行卡")
    }
    
    @objc func commitAction() {
        
        if checkCommitCondition() {

            CBToast.showToastAction()
            TSNetworkManager.request(TMSNetworkTools.bindCard(bankname: bankLocField.text!, bankusername: nameField.text!, banknum: bankField.text!, bankadress: bankAddressField.text!, bankpwd: pwdField.text!), showHUD: true, success: { (data) in
                
                CBToast.hiddenToastAction()
                
                DispatchQueue.main.asyncAfter(deadline: DispatchTime.now()+0.5) {
                    CBToast.showToastAction(message: "绑定成功")
                }
                if self.delegate != nil {
                    self.delegate?.completeBindCard()
                }
                DispatchQueue.main.asyncAfter(deadline: DispatchTime.now()+1.5) {
                    self.navigationController?.popViewController(animated: true)
                }
                
            }) { (error) in
                
                CBToast.hiddenToastAction()
            }
        }
    }
    
    func checkCommitCondition() -> Bool {
        
        guard bankField.text?.count != 0 else {
            CBToast.showToastAction(message: "请输入银行卡号")
            return false
        }
        
        guard nameField.text?.count != 0 else {
            CBToast.showToastAction(message: "请输入开户姓名")
            return false
        }
        
        guard bankLocField.text?.count != 0 else {
            CBToast.showToastAction(message: "请输入开户银行")
            return false
        }
        
        guard bankAddressField.text?.count != 0 else {
            CBToast.showToastAction(message: "请输入支行地址")
            return false
        }
        
        guard pwdField.text?.count != 0 else {
            CBToast.showToastAction(message: "请设置提现密码")
            return false
        }
        
        return true
    }
    
}

extension TSCardBindViewController {
    
    func configViews() {
        
        tableView = UITableView.init(frame: CGRect(x: 0, y: CGFloat(T_TABBARHEIGHT), width: SCREEN_WIDTH, height: SCREEN_HEIGHT - CGFloat(T_TABBARHEIGHT)), style: .plain)
        tableView.backgroundColor = UIColor.white
        tableView.separatorStyle = .none
        tableView.showsVerticalScrollIndicator = false
        self.view.addSubview(tableView)
        
        bankField = createInputField(placeholder: "请输入银行卡号")
        tableView.addSubview(bankField)
        bankField.snp.makeConstraints { (make) in
            make.left.equalTo(26)
            make.top.equalTo(49)
            make.size.equalTo(CGSize(width: SCREEN_WIDTH - 2*26, height: 50))
        }
        
        let line1 = drawLine()
        tableView.addSubview(line1)
        line1.snp.makeConstraints { (make) in
            make.left.equalTo(12)
            make.top.equalTo(bankField.snp_bottom)
            make.size.equalTo(CGSize(width: SCREEN_WIDTH-2*12, height: 1))
        }
                
        nameField = createInputField(placeholder: "请输入开户姓名")
        tableView.addSubview(nameField)
        nameField.snp.makeConstraints { (make) in
            make.left.equalTo(bankField)
            make.top.equalTo(line1.snp_bottom)
            make.size.equalTo(bankField)
        }
        
        let line2 = drawLine()
        tableView.addSubview(line2)
        line2.snp.makeConstraints { (make) in
            make.left.equalTo(line1)
            make.top.equalTo(nameField.snp_bottom)
            make.size.equalTo(line1)
        }
                        
        bankLocField = createInputField(placeholder: "请输入开户银行")
        tableView.addSubview(bankLocField)
        bankLocField.snp.makeConstraints { (make) in
            make.left.equalTo(bankField)
            make.top.equalTo(line2.snp_bottom)
            make.size.equalTo(bankField)
        }
        
        let line3 = drawLine()
        tableView.addSubview(line3)
        line3.snp.makeConstraints { (make) in
            make.left.equalTo(line1)
            make.top.equalTo(bankLocField.snp_bottom)
            make.size.equalTo(line1)
        }
                                
        bankAddressField = createInputField(placeholder: "支行地址")
        tableView.addSubview(bankAddressField)
        bankAddressField.snp.makeConstraints { (make) in
            make.left.equalTo(bankField)
            make.top.equalTo(line3.snp_bottom)
            make.size.equalTo(bankField)
        }
        
        let line4 = drawLine()
        tableView.addSubview(line4)
        line4.snp.makeConstraints { (make) in
            make.left.equalTo(line1)
            make.top.equalTo(bankAddressField.snp_bottom)
            make.size.equalTo(line1)
        }
        
        pwdField = createInputField(placeholder: "请设置提现密码")
        tableView.addSubview(pwdField)
        pwdField.snp.makeConstraints { (make) in
            make.left.equalTo(bankField)
            make.top.equalTo(line4.snp_bottom)
            make.size.equalTo(bankField)
        }
        
        let line5 = drawLine()
        tableView.addSubview(line5)
        line5.snp.makeConstraints { (make) in
            make.left.equalTo(line1)
            make.top.equalTo(pwdField.snp_bottom)
            make.size.equalTo(line1)
        }
        
        let infoLabel = UILabel.init()
        infoLabel.text = "提示：*为必填信息"
        infoLabel.textAlignment = .right
        infoLabel.textColor = BASE_RED_COLOR
        infoLabel.font = UIFont.systemFont(ofSize: 10, weight: .medium)
        tableView.addSubview(infoLabel)
        infoLabel.snp.makeConstraints { (make) in
            make.size.equalTo(CGSize(width: SCREEN_WIDTH - 2*12, height: 12))
            make.top.equalTo(line5.snp.bottom).offset(10)
        }
        
        let commitBtn = UIButton.init(type: .system)
        commitBtn.setTitle("提交", for: .normal)
        commitBtn.setTitleColor(WHITE_COLOR, for: .normal)
        commitBtn.titleLabel?.font = UIFont.systemFont(ofSize: 14, weight: .medium)
        commitBtn.backgroundColor = BASE_PURPLE_COLOR
        commitBtn.layer.cornerRadius = 24
        commitBtn.layer.masksToBounds = true
        commitBtn.addTarget(self, action: #selector(commitAction), for: .touchUpInside)
        tableView.addSubview(commitBtn)
        commitBtn.snp.makeConstraints { (make) in
            make.centerX.equalToSuperview()
            make.top.equalTo(infoLabel.snp_bottom).offset(30)
            make.size.equalTo(CGSize(width: SCREEN_WIDTH*0.682, height: 48))
        }
    }
    
    func createInputField(placeholder:String) -> UITextField {
        
        let string = "*\(placeholder)"
        let attributeStr = NSMutableAttributedString.init(string: string)
        attributeStr.addAttributes([NSAttributedString.Key.foregroundColor:UIColor.hexadecimalColor(hexadecimal: "#989898"), NSAttributedString.Key.font:UIFont.systemFont(ofSize: 14, weight: .medium)], range: NSMakeRange(0, string.count))
        attributeStr.addAttributes([NSAttributedString.Key.foregroundColor:BASE_RED_COLOR], range: NSMakeRange(0, 1))
        
        let textField = UITextField.init()
        textField.font = UIFont.systemFont(ofSize: 14, weight: .medium)
        textField.textColor = UIColor.hexadecimalColor(hexadecimal: "#282828")
        textField.attributedPlaceholder = (attributeStr.copy() as! NSAttributedString)
        textField.borderStyle = .none
        textField.clearButtonMode = .whileEditing
        return textField
    }
    
    func drawLine() -> UIView {
        let line = UIView.init()
        line.backgroundColor = UIColor.hexadecimalColor(hexadecimal: "#F5F5F5")
        return line
    }
}
